<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;

use app\admin\model\ComValidate;
use app\admin\model\ComModel;


class Auto extends Common{



	public function _initialize(){
		if(request()->isGet()){

			$action= strtolower ( Request::instance()->action() );

			switch ($action) {
		
				case 'adminlist':
					$this->assign('title',"管理员列表");
					break;
				case 'admininfo':
					$this->assign('title',"管理员信息");
					break;
				case 'rolelist':
					$this->assign('title',"角色列表");
					break;
				case 'roleinfo':
					$this->assign('title',"角色信息");
					break;
				case 'actionlist':
					$this->assign('title',"权限列表");
					break;
				case 'actioninfo':
					$this->assign('title',"权限信息");
					break;
			}

			$this->assign('controller',"auto");
		}
	}

	#管理员信息开始#
	#管理员列表#
	public function adminlist(){

		if(request()->isGet()){

			if(request()->isAjax()){
				$page=input('page');
				$limit=input('limit');

				$keyword=input('keyword');
				$start=input('start');
				$end=input('end');

				if($keyword){
					$map['name']=array('like',"%".$keyword."%");
				}
				if($start){
					$map['addtime']=array('egt',$start);
				}
				if($end){
					$map['addtime']=array('elt',$end);
				}
				if($start&&$end){
					$map['addtime']=array('between',$start.",".$end);
				}

				$list=Db::name('admin_list')->page($page.','.$limit)->where($map)->order('addtime desc')->select();
				
				foreach ($list as $key => $v) {

					$list[$key]['src']=adminthumb($v['src']);

					if($v['status']=='1'){
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-green'>正常</span>";
					}else{
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-gray'>禁用</span>"; 
					}

					$list[$key]['rolename']=ComModel::data_value('admin_role',array("roleid"=>$v['roleid']),"name");
				}
				$count=Db::name('admin_list')->where($map)->count('adminid');

				$arr=array(
					'code'=>"0",
					'count'=>$count,
					'data'=>$list
				);
				return json($arr);
			}

			$this->log("管理员列表");
			
			return $this->fetch();
		}

	}

	#编辑管理员信息#
	public function admininfo(){

		if(request()->isGet()){

			$adminid=input('adminid');

			if($adminid){
				if($adminid=="1"){
					$this->error("超级管理员不可修改！！");
				}
				$info=ComModel::data_find('admin_list',['adminid'=>$adminid]);
				if(empty($info)){
					$this->error("当前信息不存在~");
				}
				$this->assign('info',$info);
			}

			$rolelist=Db::name("admin_role")->where(['status'=>"1"])->order("addtime asc")->select();
			$this->assign('rolelist',$rolelist);
			return $this->fetch();

		}else{

			$this->log("编辑管理员信息");

			$data=input('post.');
	
			$data['status']=empty($data['status'])?"0":$data['status'];

			//修改
			if($data['adminid']){

				if($data['adminid']=="1"){
					$this->error("超级管理员不可修改！！");
				}

				$map=array(
					'nickname'=>$data['nickname'],
					'adminid'=>array('neq',$data['adminid'])
				);
				$res=ComModel::data_value('admin_list',$map,"nickname");
				if(isset($res) || $data['nickname']=="超级管理员"){
					$this->error("当前管理员名称已经存在~");
				}


				$map=array(
					'username'=>$data['username'],
					'adminid'=>array('neq',$data['adminid'])
				);
				$res=ComModel::data_value('admin_list',$map,"username");
				if(isset($res)){
					$this->error("当前账号已经存在~");
				}

				if($data['password']==""){
					unset($data['password']);
				}else{
					$data['password']=md5($data['password']."123456");
				}

				$data['uptime']=date('Y-m-d H:i:s',time());
				ComModel::data_up("admin_list",$data,url('adminlist'));

			//添加
			}else{

				$map=array(
					'username'=>$data['username']
				);
				$res=ComModel::data_value('admin_list',$map,"username");
				if(isset($res)){
					$this->error("当前账号已经存在~");
				}

				$map=array(
					'nickname'=>$data['nickname']
				);
				$res=ComModel::data_value('admin_list',$map,"nickname");
				if(isset($res) || $data['nickname']=="超级管理员"){
					$this->error("当前管理员名称已经存在~");
				}


				if($data['password']==""){
					$this->error("请输入管理员密码！");
				}

				$data['addtime']=date('Y-m-d H:i:s',time());
				ComModel::data_add("admin_list",$data,url('adminlist'));
			}
		}
	}
	#删除管理员#
	public function admindel(){

		$this->log("删除管理员");

		$adminid=input('adminid');

		if($adminid =="1"){
			$this->error("超级管理员不可修改！！");
		}

		echo ComModel::data_del('admin_list',['adminid'=>$adminid]);
	}
	#管理员信息结束#

	#角色信息开始#
	#角色列表#
	public function rolelist(){

		if(request()->isGet()){

			if(request()->isAjax()){
				$page=input('page');
				$limit=input('limit');

				$keyword=input('keyword');
				$start=input('start');
				$end=input('end');

				if($keyword){
					$map['name']=array('like',"%".$keyword."%");
				}
				if($start){
					$map['addtime']=array('egt',$start);
				}
				if($end){
					$map['addtime']=array('elt',$end);
				}
				if($start&&$end){
					$map['addtime']=array('between',$start.",".$end);
				}

				$list=Db::name('admin_role')->page($page.','.$limit)->where($map)->order('px desc')->select();
				
				foreach ($list as $key => $v) {

					$list[$key]['src']=adminthumb($v['src']);

					if($v['status']=='1'){
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-green'>正常</span>";
					}else{
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-gray'>禁用</span>"; 
					}
				}
				$count=Db::name('admin_role')->where($map)->count('roleid');

				$arr=array(
					'code'=>"0",
					'count'=>$count,
					'data'=>$list
				);
				return json($arr);
			}

			$this->log("角色列表");
			
			return $this->fetch();
		}

	}

	#编辑角色信息#
	public function roleinfo(){

		if(request()->isGet()){

			$roleid=input('roleid');
		
			if($roleid){
				$info=ComModel::data_find('admin_role',['roleid'=>$roleid]);
				if(empty($info)){
					$this->error("当前信息不存在~");
				}
				$info['auto']=explode(",", $info['auto']);
				$this->assign('info',$info);
			}

			$this->assign('actinoinfo',$this->actiondatalist());
			return $this->fetch();

		}else{

			$this->log("编辑角色信息");

			$data=input('post.');
				
			$auto="";
			if(empty($data['auto'])){
				$this->error("请选择用户权限");
			}
			foreach ($data['auto'] as $k => $v) {
				$auto.=$v.",";
			}

			$data['auto']=rtrim($auto, ",");

			$data['status']=empty($data['status'])?"0":$data['status'];

			//修改
			if($data['roleid']){

				$map=array(
					'name'=>$data['name'],
					'roleid'=>array('neq',$data['roleid'])
				);
				$res=ComModel::data_value('admin_role',$map,"name");
				if(isset($res)){
					$this->error("当前角色名称已经存在~");
				}

				$data['uptime']=date('Y-m-d H:i:s',time());
				ComModel::data_up("admin_role",$data,url('rolelist'));

			//添加
			}else{
				$res=ComModel::data_value('admin_role',array('name'=>$data['name']),"name");
				if(isset($res)){
					$this->error("当前角色名称已经存在~");
				}

				$data['addtime']=date('Y-m-d H:i:s',time());
				ComModel::data_add("admin_role",$data,url('rolelist'));
			}
		}
	}
	#删除管理员#
	public function roledel(){

		$this->log("删除角色");

		$roleid=input('roleid');

		echo ComModel::data_del('admin_role',['roleid'=>$roleid]);
	}
	#角色信息结束#
	

	#权限信息开始#
	#权限列表#
	public function actionlist(){

		if(request()->isGet()){

			if(request()->isAjax()){
				$page=input('page');
				$limit=input('limit');

				$keyword=input('keyword');
				$start=input('start');
				$end=input('end');

				if($keyword){
					$map['name']=array('like',"%".$keyword."%");
				}

				if($start){
					$map['addtime']=array('egt',$start);
				}
				if($end){
					$map['addtime']=array('elt',$end);
				}
				if($start&&$end){
					$map['addtime']=array('between',$start.",".$end);
				}

				$list=Db::name('admin_action')->page($page.','.$limit)->where($map)->order('px desc')->select();
				
				foreach ($list as $key => $v) {

					$list[$key]['src']=adminthumb($v['src']);

					if($v['status']=='1'){
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-green'>正常</span>";
					}else{
						$list[$key]['status']="<span class='layui-badge-rim layui-bg-gray'>禁用</span>"; 
					}
				}
				$count=Db::name('admin_action')->where($map)->count('actionid');

				$arr=array(
					'code'=>"0",
					'count'=>$count,
					'data'=>$list
				);
				return json($arr);
			}

			$this->log("操作列表");
			
			return $this->fetch();
		}

	}

	#编辑权限信息#
	public function actioninfo(){

		if(request()->isGet()){

			$actionid=input('actionid');
		
			if($actionid){
				$info=ComModel::data_find('admin_action',['actionid'=>$actionid]);
				if(empty($info)){
					$this->error("当前信息不存在~");
				}
				$this->assign('info',$info);
			}

			$list=ComModel::data_all('admin_action',['status'=>'1','pid'=>'0'],"px desc","actionid,name");
			$this->assign('list',$list);
			return $this->fetch();

		}else{


			$this->log("编辑角色信息");

			$data=input('post.');
		//print_r($data);exit;
			$data['status']=empty($data['status'])?"0":$data['status'];


			//修改
			if($data['actionid']){
				$data['uptime']=date('Y-m-d H:i:s',time());
				ComModel::data_up("admin_action",$data,url('actionlist'));

			//添加
			}else{
				$data['addtime']=date('Y-m-d H:i:s',time());
				ComModel::data_add("admin_action",$data,url('actionlist'));
			}
		}
	}
	#删除权限信息#
	public function actiondel(){

		$this->log("删除权限信息");

		$actionid=input('actionid');

		echo ComModel::data_del('admin_action',['actionid'=>$actionid]);
	}
	#管理员日志#
	public function loglist(){
		if(request()->isGet()){

			if(request()->isAjax()){
				$page=input('page');
				$limit=input('limit');

				$keyword=input('keyword');
				$start=input('start');
				$end=input('end');

				if($keyword){
					$map['name|content']=array('like',"%".$keyword."%");
				}

				if($start){
					$map['addtime']=array('egt',$start);
				}
				if($end){
					$map['addtime']=array('elt',$end);
				}
				if($start&&$end){
					$map['addtime']=array('between',$start.",".$end);
				}

				$list=Db::name('admin_log')->page($page.','.$limit)->where($map)->order('logid desc')->select();
				

				$count=Db::name('admin_log')->where($map)->count('logid');

				$arr=array(
					'code'=>"0",
					'count'=>$count,
					'data'=>$list
				);
				return json($arr);
			}

			$this->log("管理员日志");
			
			return $this->fetch();
		}
	}
	#权限信息结束#


	public function index(){
			
	exit;
		//1.获取所有控制
		$dir="./app/admin/controller/";
		$data=array();
		$con_arr=array("Common","error","Login","SITE"); //排除
		foreach (scandir($dir) as $k => $v) {
			if(preg_match('/[A-Za-z]+.php/', $v,$res)/* && !in_array($v,$con_arr)*/){
				//if(!in_array(trim($v),$con_arr)){
				 $controller=substr($v, 0,-4);
				 if(!in_array($controller,$con_arr)){

					if(Db::name('admin_action')->where(array('value'=>$controller,
						'type'=>'控制器'))->count() >= "1"){
						continue;
					}

					$insert_data=array(
						'pid'=>"0",
						'value'=>$controller,
						'type'=>'控制器',
						'addtime'=>date('Y-m-d H:i:s')
					);
					$pid=Db::name("admin_action")->insertGetId($insert_data);

					//2.获取所有控制器
					$res=get_class_methods(controller($controller));
					foreach ($res as $key => $val) {

						if($val=="__construct"){

							continue;
						}
					 $cont=file_get_contents("./app/admin/controller/".$controller.".php");
					// echo $cont;exit;
					 preg_match("/#(.*?)#\r\n(.*?)".$val."/", $cont, $arr);
					 if(!empty($arr)){
					 	$insert_data=array(
							'pid'=>$pid,
							'name'=>$arr[1],
							'value'=>$controller."/".$val,
							'type'=>'操作',
							'addtime'=>date('Y-m-d H:i:s')
						);

						if(Db::name('admin_action')->where(array('value'=>$val,
						'type'=>'操作'))->count() >= "1"){
							continue;
						}

						Db::name("admin_action")->insertGetId($insert_data);

					 	//echo $arr[1]."<br/>";
					 }

					}
					//array_push($data,substr($v, 0,-4));
				}
			}
		}
		echo "处理结束";
	}


   



}